class Solution:
  def findLargestHex(self, s: str) -> int:
    if s == '':
      return 0
    s_list = [i for i in s]
    valid = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    dp = [0 for i in range(len(s)+1)]
    # 去除不合规的前导字符
    while s_list[0] not in valid:
      s_list.pop(0)
    square = 0
    for i in range(len(s)-1, -1, -1):
      if s_list[i] in valid:
        char = s_list[i]
        convert = {
          '0': 0,
          '1': 1,
          '2': 2,
          '3': 3,
          '4': 4,
          '5': 5,
          '6': 6,
          '7': 7,
          '8': 8,
          '9': 9,
          'A': 10,
          'B': 11,
          'C': 12,
          'D': 13,
          'E': 14,
          'F': 15
        }
        dp[i] = dp[i+1] + (16 ** square) * convert[char]
        square = square + 1
      else:
        square = 0
        dp[i] = 0
    return max(dp)


if __name__ == "__main__":
  solution = Solution()
  print(solution.findLargestHex('012345BZ16'))
